<?php

/**
 * Flickr Photosets - Photo Page
 * Displays a single photo either in a photoset or the photostream.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 2.0
 * @date 9/22/07
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since File available since Release 1.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr
*/
$fp = new FlickrPhotosetsCanvas();

// Require the photo id
$fp->require_variables($_GET['pid']);

// Get information on the photo
$photo = $fp->flickr->photos_getInfo($_GET['pid']);
$exif_info = $fp->flickr->photos_getExif($photo['id']);
$favorite = $fp->flickr->photos_getFavorites($photo['id']);
$geo = $fp->flickr->photos_geo_getLocation($photo['id']);
$sizes = $fp->flickr->photos_getSizes($photo['id']);

// Get the Flickr user info
// May add this to FlickrPhotosets::init_flickr()
$flickr_user = $fp->flickr->people_getInfo($fp->nsid);

// Get the size of the medium photo
foreach ($sizes as $size) {
	if ($size['label'] == 'Medium') {
		$photo_width = $size['width'];
		$photo_height = $size['height'];
		break;
	}
}

// Get the set id
if (isset($_GET['sid'])) {
	$sid = $_GET['sid'];
}

// This is in the photostream - INCOMPLETE
// Need to make this section work with privacy settings
if (empty($sid)) {
	
	// Get the index and count
	$photos = $fp->flickr->people_getPublicPhotos($fp->nsid, '', 500);
	$photo_count = $photos['total'];
	
	$photos_all = $photos['photo'];
	for ($i = 1; $i < $photos['pages']; $i++) {
		$photos_page = $fp->flickr->people_getPublicPhotos($fp->nsid, '', 500, $i+1);
		$photos_all = array_merge($photos_all, $photos_page['photo']);
	}
	
	$photos_all = array_reverse($photos_all);
	$current_photo_index = 0;
	foreach ($photos_all as $photo_index) {
		if ($photo_index['id'] == $photo['id']) {
			break;
		}
		$current_photo_index++;
	}
	$current_photo = $current_photo_index + 1;
	
	// Reset the photos array for use
	$photos['photo'] = $photos_all;
	
	// Get the context of the photo
	$context = $fp->flickr->photos_getContext($photo['id']);
	
	// Get photostream information
	$title = 'Photostream';
	
}
// This is in a set
else {
	
	// Get all of the photos in the set
	$photos = $fp->flickr->photosets_getPhotos($sid);
	
	// Remove photos that do not match the privacy settings
	$photos['photo'] = $fp->remove_photos($photos['photo']);
	
	// Get the index and count
	$current_photo_index = 0;
	foreach ($photos['photo'] as $photo_index) {
		if ($photo_index['id'] == $photo['id']) {
			break;
		}
		$current_photo_index++;
	}
	$current_photo = $current_photo_index + 1;
	$photo_count = count($photos['photo']);
	
	// Get the context for the photo
	$photos['photo'] = array_values($photos['photo']);
	$context['nextphoto']['id'] = $photos['photo'][$current_photo_index+1]['id'];
	$context['prevphoto']['id'] = $photos['photo'][$current_photo_index-1]['id'];
	
	// Get photoset information
	$photoset = $fp->flickr->photosets_getInfo($sid);
	$title = $photoset['title'];
	
}

// Determine the page that the photo is on in the photoset index
if (isset($photoset)) {
	//$page = ceil(($current_photo_index + 1) / $fp->photos_per_page);
	$photoset_page = ceil($current_photo / $fp->photos_per_page);
}
else {
	$page = ceil(($photo_count / $fp->photos_per_page) - ($current_photo_index / $fp->photos_per_page));
}

// Determine the previous and next photos
$prev_pid = $context['prevphoto']['id'];
if ($prev_pid == 0) {
	$prev_pid = $photos['photo'][$photo_count-1]['id'];
}
$next_pid = $context['nextphoto']['id'];
if ($next_pid == 0) {
	$next_pid = $photos['photo'][0]['id'];
}

?>

<fb:title><?=$title?> | <?=$photo['title']?></fb:title>

<?=$fp->print_media_header($title)?>

<div class="single_photo_header clearfix">
	<h2><?=$photo['title']?>&nbsp;</h2>
	<h4>
		Photo <?=$current_photo?> of <?=$photo_count?>
		<span class="pipe">|</span>
		<?php if (isset($photoset)) { ?>
			<a href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/page/<?=$photoset_page?>/">Back to Photoset</a>
		<?php } else { ?>
			<a href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/page/<?=$page?>/">Back to Photostream</a>
		<?php }?>
	</h4>	
	<ul class="pagerpro navlinks">
		<li><a href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/<?=$prev_pid?><?php if (isset($photoset)) { ?>/in/set-<?=$photoset['id']?><?php } ?>/">Previous</a></li>
		<li><a href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/<?=$next_pid?><?php if (isset($photoset)) { ?>/in/set-<?=$photoset['id']?><?php } ?>/">Next</a></li>
	</ul>
</div>

<div class="photoborder">
	
	<?php if ($photo['media'] == 'video') {
		
		// Make the video no wider than 550 px
		$width = $photo['video']['width'];
		$height = $photo['video']['height'];
		$ratio = $width / $height;
		
		$video_width = round(min(550, $width));
		$video_height = round($video_width / $ratio);
		
	?>
		<div class="video">
			<fb:swf swfsrc="http://www.flickr.com/apps/video/stewart.swf?v=1.169" height="<?=$video_height?>" width="<?=$video_width?>" flashvars="intl_lang=en-us&amp;photo_secret=<?=$photo['secret']?>&amp;photo_id=<?=$photo['id']?>" />
		</div>
	<?php } else { ?>
		<div style="margin: 10px auto; padding: 0; width: <?=$photo_width?>px;">
		<a href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/<?=$next_pid?><?php if (isset($photoset)) { ?>/in/set-<?=$photoset['id']?><?php } ?>/">
			<div style="position: relative; width: <?=$photo_width?>px;">
			<?php if (!$sizes['candownload']) {
				echo '<img src="'.$fp->app_call_url.'/images/spaceball.gif" width="'.$photo_width.'px" height="'.$photo_height.'px" style="position: absolute; display: block; border: 0; background-color: transparent;" />';
			} ?>
			<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'medium')?>" />
			</div>
		</a>
		</div>
	<?php } ?>
	
	<div class="photocaptionleft">
		
		<div class="photoinalbum">
			<?php if (isset($photoset)) { ?>
				<span class="text">From the photoset:</span><br />
				<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$sid?>/"><?=$photoset['title']?></a>
				<span class="text"> by </span>
				<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>"><?=$flickr_user['username']?></a>
			<?php } else { ?>
				<span class="text">From <a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>"><?=$flickr_user['username']?></a>'s Photostream</span>
			<?php } ?>
		</div>
		
		<div class="photocaption">
			<?=$photo['description']?>
		</div>
		<div class="phototags">
			<?=$fp->get_tag_string($photo)?>
		</div>
		
		<div class="additional_info clearfix">
			
			<div class="photodate">
				<?php // Change date taken based on granularity
				$granularity = $photo['dates']['takengranularity'];
				$date_taken = strtotime($photo['dates']['taken']);

				if ($granularity == 6) {
					echo 'Taken some time in <a href="'.$fp->flickr->urls_getUserPhotos($fp->nsid).'archives/undated/'.date("Y/", $date_taken).'">'.date("Y", $date_taken).'</a>';
				}
				else if ($granularity == 4) {
					echo 'Taken in <a href="'.$fp->flickr->urls_getUserPhotos($fp->nsid).'archives/undated/'.date("Y/m/", $date_taken).'">'.date("F Y", $date_taken).'</a>';
				}
				else {
					echo 'Taken on <a href="'.$fp->flickr->urls_getUserPhotos($fp->nsid).'archives/date-taken/'.date("Y/m/d/", $date_taken).'">'.date("F j, Y", $date_taken).'</a>';
				} ?>
			</div>
			
		</div>
		
	</div>
	
</div>

<div class="footer">
	
	<div class="right">
		<?php if ($photo['media'] == 'video') { ?>
			<fb:share-button class="meta">
				<meta name="title" content="Flickr Video: <?=$photo['title']?>" />
				<meta name="description" content="<?=htmlentities($photo['description'])?>" />
				<link rel="image_src" href="<?=$fp->flickr->buildPhotoUrl($photo, 'thumbnail')?>" />
				<link rel="target_url" href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/<?=$photo['id']?>/" />
			</fb:share-button>
		<?php } else { ?>
			<fb:share-button class="meta">
				<meta name="title" content="Flickr Photo: <?=$photo['title']?>" />
				<meta name="description" content="<?=htmlentities($photo['description'])?>" />
				<link rel="image_src" href="<?=$fp->flickr->buildPhotoUrl($photo, 'thumbnail')?>" />
				<link rel="target_url" href="<?=$fp->app_url?>/photos/<?=$fp->uid?>/<?=$photo['id']?>/" />
			</fb:share-button>
		<?php } ?>
		
		<div class="photoactions">
			<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?><?=$photo['id']?>/">Flickr Photo Page</a>
			<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?><?=$photo['id']?>#reply">Flickr Comments</a>
			<?php if (!empty($geo)) { 
				echo '<a href="'.$fp->flickr->urls_getUserPhotos($fp->nsid).$photo['id'].'/map/">Flickr Map</a>';
			} ?>
		</div>
	</div>
	
	<div class="photoinfo">
		
		<h4 style="padding-top:5px;">Additional Information</h4>
		
		<p>
			<?php if (!empty($geo)) {
				$loc = $fp->flickr->getFriendlyGeoData($geo['location']['latitude'], $geo['location']['longitude']);
				echo 'Taken <a href="'.$fp->app_url.'/photos/'.$fp->uid.'/'.$photo['id'].'/map/">'.$loc['msg'].'</a><br/>';
			} ?>
			
			<?php if (!empty($exif[1]['raw'])) { ?>
				Taken with a <?=ucwords(strtolower($exif[1]['raw']))?><br/>
			<?php } ?>
			
			Posted to Flickr on <a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>archives/date-posted/<?=date("Y/m/d/", $photo['dates']['posted'])?>"><?=date("F j, Y", $photo['dates']['posted']);?></a><br/>
			
			<?php if ($favorite['total'] > 0) { ?>
				<?php if ($favorite['total'] == 1) { ?>
					<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid).$pid?>/favorites/"><?=$favorite['total']?> person</a> calls this photo a favorite
				<?php } else { ?>
					<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid).$pid?>/favorites/"><?=$favorite['total']?> people</a> call this photo a favorite
				<?php } ?>
			<?php }?>
		</p>
		
	</div>
	
	<div class="photocomment">
		
		<?=$fp->print_comments($fp->flickr->photos_comments_getList($photo['id']))?>
		
	</div>
	
</div>

<div class="clearfix"></div>

<?=$fp->echo_footer(true)?>